package com.maxsalad.baidu.util;

import com.maxsalad.common.util.EncryptUtil;
import com.maxsalad.common.util.JsonUtil;

import java.security.NoSuchAlgorithmException;

/**
 * Created by 2014-400 on 2016-03-07.
 */
public class SignUtil {
    /**
     * 计算sign
     *
     * @param json
     * @return
     * @throws NoSuchAlgorithmException
     */
    public static String getSign(String json) throws NoSuchAlgorithmException {
        String signJson = json.replace("/", "\\/");
        signJson = EncryptUtil.chinaToUnicode(signJson);

        return EncryptUtil.getMD5(signJson);
    }

    /**
     * 计算sign
     *
     * @param obj
     * @return
     * @throws NoSuchAlgorithmException
     */
    public static String getSign(Object obj) throws NoSuchAlgorithmException {
        String json = JsonUtil.serialize(obj);
        return getSign(json);
    }
}
